/**
 * Loading状态管理工具
 * 提供全局和局部的Loading状态管理
 */

// 全局Loading计数
let globalLoadingCount = 0
let loadingTimer = null

/**
 * 显示全局Loading
 * @param {string} title - Loading提示文字
 */
export const showLoading = (title = '加载中...') => {
  globalLoadingCount++

  // 避免频繁显示/隐藏Loading
  if (globalLoadingCount === 1) {
    // 延迟50ms显示，避免闪烁
    loadingTimer = setTimeout(() => {
      uni.showLoading({
        title,
        mask: true
      })
    }, 50)
  }
}

/**
 * 隐藏全局Loading
 */
export const hideLoading = () => {
  globalLoadingCount = Math.max(0, globalLoadingCount - 1)

  if (globalLoadingCount === 0) {
    if (loadingTimer) {
      clearTimeout(loadingTimer)
      loadingTimer = null
    }

    uni.hideLoading()
  }
}

/**
 * 创建局部Loading状态
 * @returns {Object} Loading状态管理对象
 */
export const createLoadingState = () => {
  const loading = ref(false)

  const startLoading = (title = '加载中...') => {
    loading.value = true
    showLoading(title)
  }

  const endLoading = () => {
    loading.value = false
    hideLoading()
  }

  const withLoading = async (promise, title = '加载中...') => {
    try {
      startLoading(title)
      return await promise
    } finally {
      endLoading()
    }
  }

  return {
    loading: readonly(loading),
    startLoading,
    endLoading,
    withLoading
  }
}

/**
 * 页面级Loading组合式函数
 * @param {Object} options - 配置选项
 * @returns {Object} Loading状态和方法
 */
export const useLoading = (options = {}) => {
  const {
    defaultTitle = '加载中...',
    immediate = false
  } = options

  const loading = ref(immediate)
  const loadingTitle = ref(defaultTitle)

  const startLoading = (title = defaultTitle) => {
    loading.value = true
    loadingTitle.value = title
    showLoading(title)
  }

  const endLoading = () => {
    loading.value = false
    hideLoading()
  }

  const toggleLoading = (state, title = defaultTitle) => {
    if (state) {
      startLoading(title)
    } else {
      endLoading()
    }
  }

  const withLoading = async (promise, title = defaultTitle) => {
    try {
      startLoading(title)
      return await promise
    } finally {
      endLoading()
    }
  }

  return {
    loading: readonly(loading),
    loadingTitle: readonly(loadingTitle),
    startLoading,
    endLoading,
    toggleLoading,
    withLoading
  }
}

/**
 * 防抖Loading状态
 * 避免Loading状态快速切换造成的闪烁
 * @param {number} delay - 延迟时间（毫秒）
 * @returns {Object} Loading状态和方法
 */
export const useDebounceLoading = (delay = 200) => {
  const loading = ref(false)
  let debounceTimer = null

  const startLoading = (title = '加载中...') => {
    clearTimeout(debounceTimer)
    debounceTimer = setTimeout(() => {
      loading.value = true
      showLoading(title)
    }, delay)
  }

  const endLoading = () => {
    clearTimeout(debounceTimer)
    loading.value = false
    hideLoading()
  }

  const withLoading = async (promise, title = '加载中...') => {
    try {
      startLoading(title)
      return await promise
    } finally {
      endLoading()
    }
  }

  return {
    loading: readonly(loading),
    startLoading,
    endLoading,
    withLoading
  }
}

// 引入Vue的响应式API
import { ref, readonly } from 'vue'
